/**
 * Copyright &copy; com.smooth.common
 */
package com.smooth.site.sx.mag.biz.job.service;

import java.util.Date;
import java.util.List;

import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.smooth.common.site.common.persistence.Page;
import com.smooth.common.site.common.utils.IdGenerater;
import com.smooth.dao.gen.BizJobMapper;
import com.smooth.dao.gen.bean.BizCompany;
import com.smooth.dao.gen.bean.BizJob;
import com.smooth.dao.gen.bean.BizJobExample;
import com.smooth.site.sx.mag.biz.comp.service.BizCompanyService;

/**
 * 职位Service
 * @author bm
 * @version 2019-02-10
 */
@Service
@Transactional(readOnly = true)
public class BizJobService{
	Logger log = LoggerFactory.getLogger(BizJobService.class);
	@Autowired
	BizJobMapper bizJobMapper;
	@Autowired
	IdGenerater idGenerater;
	
	public BizJob get(String no) {
		return bizJobMapper.selectByPrimaryKey(no);
	}
	
	public List<BizJob> findList(BizJob bizJob) {
		BizJobExample bizJobExample = new BizJobExample();
		BizJobExample.Criteria criteria = bizJobExample.createCriteria();
		
		return bizJobMapper.selectByExample(bizJobExample);
	}
	
	public Page<BizJob> findPage(Page<BizJob> page, BizJob bizJob) {
		BizJobExample bizJobExample = new BizJobExample();
		BizJobExample.Criteria criteria = bizJobExample.createCriteria();
		
		bizJobExample.setPage(page);
		
		List<BizJob> dlist = bizJobMapper.selectByExample(bizJobExample);
		page.setList(dlist);
		page.setCount(bizJobMapper.countByExample(bizJobExample));
		
		return page;
	}
	@Autowired
	BizCompanyService bizCompanyService;
	
	@Transactional(readOnly = false)
	public BizJob save(BizJob bizJob) {
		if(StringUtils.isNotBlank(bizJob.getCompId())) {
			BizCompany bizCompany = bizCompanyService.get(bizJob.getCompId());
			bizJob.setCompName(bizCompany.getCompName());
		}
		if(StringUtils.isNotEmpty(bizJob.getId())&&get(bizJob.getId())!=null) {
			int optNum = bizJobMapper.updateByPrimaryKeySelective(bizJob);
			if(optNum==1) {
				log.info("更新 student 数据【"+bizJob.toString()+"】成功");
			}else {
				log.error("更新 student 数据【"+bizJob.toString()+"】失败");
			}
			return bizJobMapper.selectByPrimaryKey(bizJob.getId());
		}else {
			Date date = new Date();
			bizJob.setCreateDate(date);
			bizJob.setId(idGenerater.genClsNo("bizJob"));
			bizJob.setUpdateDate(date);
			int optNum =  bizJobMapper.insertSelective(bizJob);
			if(optNum==1) {
				log.info("插入 student 数据【"+bizJob.toString()+"】成功");
			}else {
				log.error("插入 student 数据【"+bizJob.toString()+"】失败");
			}
			return bizJob;
		}
	}
	
	@Transactional(readOnly = false)
	public void delete(BizJob bizJob) {
		bizJobMapper.deleteByPrimaryKey(bizJob.getId());
	}
	
}